package com.dream.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dream.classcontrol.MainActivity;
import com.dream.classcontrol.R;
import com.dream.config.DevUIIDConfig;
import com.dream.sdkmanager.SDKManager;


public class TouyingFragment extends android.app.Fragment implements View.OnClickListener {

    private ImageView touyingUp;
    private ImageView touyingLeft;
    private ImageView touyingBottom;
    private ImageView touyingRight;
    private ImageView touyingStart;
    private ImageView touyingXinghao;
    private ImageView touyingBig;
    private ImageView touyingSmall;
    private ImageView touyingAuto;
    private ImageView touyingMenu;
    private TextView touyingSure;
    private TextView touyingReturn;
    private TextView touyingCancle;

    private ImageView[] controlImageViews;
    private int[] controlImageViewIDs_selected;
    private int[] controlImageViewIDs_unselect;
    private ImageView[] imageViews;

    private int startState=0;
    private int autoState=0;

    private MainActivity mainActivity;
    private SDKManager sdkManager;

    public TouyingFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mainActivity= (MainActivity) activity;
        sdkManager=mainActivity.getSdkManager();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_touying, container, false);
        initView(view);
        setOnClick();
        return view;
    }

    public void initView(View view){
        touyingUp = (ImageView) view.findViewById(R.id.touying_up);
        touyingLeft = (ImageView) view.findViewById(R.id.touying_left);
        touyingBottom = (ImageView) view.findViewById(R.id.touying_bottom);
        touyingRight = (ImageView) view.findViewById(R.id.touying_right);
        touyingStart = (ImageView) view.findViewById(R.id.touying_start);
        touyingXinghao = (ImageView) view.findViewById(R.id.touying_xinghao);
        touyingBig = (ImageView) view.findViewById(R.id.touying_big);
        touyingSmall = (ImageView) view.findViewById(R.id.touying_small);
        touyingAuto = (ImageView) view.findViewById(R.id.touying_auto);
        touyingMenu = (ImageView) view.findViewById(R.id.touying_menu);
        touyingSure = (TextView) view.findViewById(R.id.touying_sure);
        touyingReturn = (TextView) view.findViewById(R.id.touying_return);
        touyingCancle = (TextView) view.findViewById(R.id.touying_cancle);
        controlImageViews=new ImageView[]{touyingUp,touyingLeft,touyingBottom,touyingRight};
        controlImageViewIDs_selected=new int[]{R.drawable.bg_touying_control_up_selected,R.drawable.bg_touying_control_left_selected,R.drawable.bg_touying_control_down_selected,R.drawable.bg_touying_control_right_selected};
        controlImageViewIDs_unselect=new int[]{R.drawable.bg_touying_control_up_unselect,R.drawable.bg_touying_control_left_unselect,R.drawable.bg_touying_control_down_unselect,R.drawable.bg_touying_control_right_unselect};
        imageViews=new ImageView[]{touyingStart,touyingXinghao,touyingBig,touyingSmall,touyingAuto,touyingMenu};
    }

    public void setOnClick(){
        touyingUp.setOnClickListener(this);
        touyingLeft.setOnClickListener(this);
        touyingBottom.setOnClickListener(this);
        touyingRight.setOnClickListener(this);
        touyingStart.setOnClickListener(this);
        touyingXinghao.setOnClickListener(this);
        touyingBig.setOnClickListener(this);
        touyingSmall.setOnClickListener(this);
        touyingAuto.setOnClickListener(this);
        touyingMenu.setOnClickListener(this);
        touyingSure.setOnClickListener(this);
        touyingReturn.setOnClickListener(this);
        touyingCancle.setOnClickListener(this);
    }

    public void sendTouYingKey(int key){
        if (sdkManager.getLandingSate()){
            sdkManager.senKey(DevUIIDConfig.touyingyi,key);
        }else {
            Toast.makeText(getActivity(),"请登录后重试",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.touying_up:
                sendTouYingKey(0);
                break;
            case R.id.touying_left:
                sendTouYingKey(0);
                break;
            case R.id.touying_bottom:
                sendTouYingKey(0);
                break;
            case R.id.touying_right:
                sendTouYingKey(0);
                break;
            case R.id.touying_start:
                if (startState==0){
                    touyingStart.setImageResource(R.drawable.bg_touying_start_selected);
                    startState++;
                    sendTouYingKey(0);
                }else {
                    touyingStart.setImageResource(R.drawable.bg_touying_start_unselect);
                    startState--;
                    sendTouYingKey(0);
                }
                break;
            case R.id.touying_xinghao:
                sendTouYingKey(0);
                break;
            case R.id.touying_big:
                sendTouYingKey(0);
                break;
            case R.id.touying_small:
                sendTouYingKey(0);
                break;
            case R.id.touying_auto:
                if (autoState==0){
                    touyingAuto.setImageResource(R.drawable.bg_touying_auto_selected);
                    autoState++;
                    sendTouYingKey(0);
                }else {
                    touyingAuto.setImageResource(R.drawable.bg_touying_auto_unselect);
                    autoState--;
                    sendTouYingKey(0);
                }
                break;
            case R.id.touying_menu:
                sendTouYingKey(0);
                break;
            case R.id.touying_sure:
                sendTouYingKey(0);
                break;
            case R.id.touying_return:
                sendTouYingKey(0);
                break;
            case R.id.touying_cancle:
                sendTouYingKey(0);
                break;
        }
    }

    public void setControlBg(int index){
        for (int i = 0; i < controlImageViews.length; i++) {
            if (index==i){
                controlImageViews[i].setImageResource(controlImageViewIDs_selected[i]);
            } else {
                controlImageViews[i].setImageResource(controlImageViewIDs_unselect[i]);
            }
        }
    }
}
